﻿@page "/categories"
@using Microsoft.AspNetCore.Identity;
@using BlazorBlogs.Data;
@using BlazorBlogs.Data.Models;
@inject UserManager<ApplicationUser> _UserManager
@inject RoleManager<IdentityRole> _RoleManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inherits OwningComponentBase<BlogsService>
@if (CurrentUser.IsInRole(ADMINISTRATION_ROLE))
{
    <div>
        <h1>Category Administration <button type="button" class="btn btn-link" @onclick="OpenCategoryManagerControl">[Edit Categories]</button></h1>
    </div>
    <div class="form-group">
        <RadzenDropDown AllowClear="false"
                        AllowFiltering="true"
                        FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                        @bind-Value="SelectedBlog" Multiple="false"
                        Placeholder="Select Blog..."
                        Data="@ColBlogEntries" TextProperty="BlogTitle"
                        Style="width:500px;"
                        Change="@(args => ChangeBlog(args, "DropDown"))" />
        <br />
        @if (SelectedBlog != null)
        {
            <div class="form-group">
                <RadzenDropDown AllowClear="true"
                                AllowFiltering="true"
                                FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                                @bind-Value="selectedBlogCategorys" Multiple="true"
                                Placeholder="Select Catagories..."
                                Data="@colCategorys" TextProperty="Title"
                                ValueProperty="CategoryId"
                                Style="width:400px;" />
            </div>
            <br />
            <div class="form-group">
                <button class="btn btn-success"
                        @onclick="SaveBlog">
                    Update
                </button>
            </div>
        }
        <p style="color:red">@strError</p>
    </div>

    <CategoryManager @ref="CategoryManagerControl"
                     CategoryUpdated="UpdateCategories" />
}
@code {
    // AuthenticationState is available as a CascadingParameter
    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }

    string strError = "";
    string ADMINISTRATION_ROLE = "Administrators";

    public bool ConFirmDeletePopup = false;

    public System.Security.Claims.ClaimsPrincipal CurrentUser;

    List<Blogs> ColBlogEntries = new List<Blogs>();
    Blogs SelectedBlog;

    private CategoryManager CategoryManagerControl;

    List<CategoryDTO> colCategorys = new List<CategoryDTO>();
    IEnumerable<string> selectedBlogCategorys = new string[] { };

    protected override async Task OnInitializedAsync()
    {
        // Get the current user
        CurrentUser = (await authenticationStateTask).User;

        ColBlogEntries = await @Service.GetAllBlogsAsync(CurrentUser.Identity.Name.ToLower());
        colCategorys = await @Service.GetCategorysAsync();
    }

    void ChangeBlog(object value, string name)
    {
        // Set the selected Blog Categorys
        List<string> BlogCatagories = SelectedBlog.BlogCategory.Select(x => x.CategoryId.ToString()).ToList();
        selectedBlogCategorys = BlogCatagories.ToArray<string>();
        strError = "";
        StateHasChanged();
    }

    async Task SaveBlog()
    {
        try
        {
            var result =
            await @Service.UpdateBlogCategoriesAsync(SelectedBlog, selectedBlogCategorys);

            strError = "Updated!";
            StateHasChanged();
        }
        catch (Exception ex)
        {
            strError = ex.GetBaseException().Message;
        }
    }

    //CategoryManagerControl

    void OpenCategoryManagerControl()
    {
        // Open CategoryManagerControl
        CategoryManagerControl.SetShowManager(true);
    }

    async Task UpdateCategories()
    {
        colCategorys = await @Service.GetCategorysAsync();
    }

    async Task ClosePopup()
    {
        // Refresh collection
        await UpdateCategories();
    }
}
